#include "WhiteBoardFile.h"

WhiteBoardFile::WhiteBoardFile(QObject *parent) : QObject(parent)
{
    init();
}

void WhiteBoardFile::init()
{
    PaintDir = APPPATH + "/" + PAINTFILE::DIRNAME;
    if(!DirIsExist(PaintDir))
        CreateDir(PaintDir);
}

bool WhiteBoardFile::DirIsExist(const QString &path)
{
    QDir dir(path);
    return dir.exists();
}

bool WhiteBoardFile::CreateDir(const QString &path)
{
    QDir dir(path);
    return dir.mkdir(path);
}

void WhiteBoardFile::SaveImage(const QImage &Image, const QString &Path)
{
    QFile fp(Path);
    if(fp.open(QFile::WriteOnly))
    {
        QImage image = Image;
        QByteArray imageData;
        QBuffer buffer(&imageData);
        buffer.open(QIODevice::WriteOnly);
        image.save(&buffer, "PNG");
        for(int byteArrayIndex=0;byteArrayIndex<imageData.size();imageData[byteArrayIndex] = imageData[byteArrayIndex] ^ 66,++byteArrayIndex);
        fp.write(imageData);
        fp.flush();
        fp.close();
    }
}

void WhiteBoardFile::SaveData(const QMap<unsigned int, PW::PROPERTYDATA> *data, const QColor &CurBckColor, const QString &path)
{
    QFile fp(path);
    if(fp.open(QIODevice::WriteOnly|QIODevice::Text))
    {
        QJsonObject JsonObjectMain;

        // 加入背景色
        int CurBckColor_r,CurBckColor_g,CurBckColor_b;
        CurBckColor.getRgb(&CurBckColor_r,&CurBckColor_g,&CurBckColor_b);
        QJsonArray JsonArray_PaintBackgroundColor;
        JsonArray_PaintBackgroundColor.append(CurBckColor_r);
        JsonArray_PaintBackgroundColor.append(CurBckColor_g);
        JsonArray_PaintBackgroundColor.append(CurBckColor_b);
        JsonObjectMain.insert("BackgroundColor",JsonArray_PaintBackgroundColor);

        // 循环写入数据
        for(auto itData = data->begin();itData != data->end(); ++itData)
        {
            QJsonObject JsonObject_Single;

            PW::PROPERTYDATA pd = itData.value();

            // Pen - Color
            int PenColorR,PenColorG,PenColorB;
            pd.pen.color().getRgb(&PenColorR,&PenColorG,&PenColorB);
            QJsonArray JsonArray_PenColor;
            JsonArray_PenColor.append(PenColorR);
            JsonArray_PenColor.append(PenColorG);
            JsonArray_PenColor.append(PenColorB);
            // Pen - Width
            int PenWidth = pd.pen.width();
            // Pen
            QJsonObject JsonObject_Pen;
            JsonObject_Pen.insert("Color",JsonArray_PenColor);
            JsonObject_Pen.insert("Width",PenWidth);

            // BeginEndPoint - Begin
            QJsonArray JsonArray_Begin;
            JsonArray_Begin.append(pd.BeginEndPoint.first.x());
            JsonArray_Begin.append(pd.BeginEndPoint.first.y());
            // BeginEndPoint - End
            QJsonArray JsonArray_End;
            JsonArray_End.append(pd.BeginEndPoint.second.x());
            JsonArray_End.append(pd.BeginEndPoint.second.y());
            // BeginEndPoint
            QJsonArray JsonArray_BeginEndPoint;
            JsonArray_BeginEndPoint.append(JsonArray_Begin);
            JsonArray_BeginEndPoint.append(JsonArray_End);

            // PaintData
            QJsonArray JsonArray_PaintData;
            for(auto itPaintData = pd.PaintData.begin();itPaintData != pd.PaintData.end();++itPaintData)
            {
                QJsonArray JsonArray_SinglePaintData;
                JsonArray_SinglePaintData.append(itPaintData->x());
                JsonArray_SinglePaintData.append(itPaintData->y());
                JsonArray_PaintData.append(JsonArray_SinglePaintData);
            }

            // 写入本次Json
            JsonObject_Single.insert("Pen",JsonObject_Pen);
            JsonObject_Single.insert("IsShow",pd.IsShow);
            JsonObject_Single.insert("ModelType",static_cast<int>(pd.Mt));
            JsonObject_Single.insert("PLOYGONTYPE",static_cast<int>(pd.Pt));
            JsonObject_Single.insert("BeginEndPoint",JsonArray_BeginEndPoint);
            JsonObject_Single.insert("PaintData",JsonArray_PaintData);

            JsonObjectMain.insert(QString::number(itData.key()),JsonObject_Single);
        }
        // 将Json转为字符串
        QJsonDocument document;
        document.setObject(JsonObjectMain);
        QByteArray byteArray = document.toJson(QJsonDocument::Compact);

        for(int byteArrayIndex=0;byteArrayIndex<byteArray.size();byteArray[byteArrayIndex] = byteArray[byteArrayIndex] ^ 66,++byteArrayIndex);
        fp.write(byteArray);

        fp.flush();
        fp.close();
    }
}

QByteArray WhiteBoardFile::ReadJsonData(const QString &jp)
{
    QByteArray data;
    QFile fp(jp);
    if(fp.open(QFile::ReadOnly | QIODevice::Text))
    {
        data = fp.readAll();
        for(int byteArrayIndex=0;byteArrayIndex<data.size();data[byteArrayIndex] = data[byteArrayIndex] ^ 66,++byteArrayIndex);
        fp.close();
    }
    return data;
}

void WhiteBoardFile::SavePaintData(const QMap<unsigned int, PW::PROPERTYDATA> *data, const QColor &bck, const QImage &image, const QString &path)
{
    if(data && !data->isEmpty() && !image.isNull())
    {
        SaveImage(image,path+PAINTFILE::IMAGEEN);
        SaveData(data,bck,path+PAINTFILE::FILEEN);
    }
    data = nullptr;
}

QMap<QString,QPair<QString,QString>> WhiteBoardFile::FindQuote()
{
    QuoteFile.clear();
    QDir dir(PaintDir);
    QStringList nameFilters;
    nameFilters << "*.pat" << "*.png";
    QStringList files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
    foreach (QString file, files)
    {
        QString baseName = file.left(file.size()-PAINTFILE::FILEEN.size());
        if(!QuoteFile.contains(baseName))
        {
            QPair<QString,QString> pair;
            pair.first = baseName + PAINTFILE::IMAGEEN;
            pair.second = baseName + PAINTFILE::FILEEN;
            QuoteFile[baseName] = pair;
        }
    }
    return QuoteFile;
}

void WhiteBoardFile::DeleteSingleWidgetInfo(const QString &tag)
{
    if(!QuoteFile.isEmpty() && QuoteFile.contains(tag))
    {
        QFile::remove(APPPATH+"/"+PAINTFILE::DIRNAME+"/"+tag+PAINTFILE::FILEEN);
        QFile::remove(APPPATH+"/"+PAINTFILE::DIRNAME+"/"+tag+PAINTFILE::IMAGEEN);
    }
}
